package com.zzyl.nursing.service.impl;

import java.lang.invoke.VarHandle;
import java.math.BigDecimal;
import java.security.Policy;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingLevelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;

/**
 * 护理等级Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-07
 */
@Slf4j
@Service
public class NursingLevelServiceImpl implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        return nursingLevelMapper.selectNursingLevelById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        stringRedisTemplate.delete(NURSING_LEVEL_KEY + "listAll");

        return nursingLevelMapper.insertNursingLevel(nursingLevel);
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        log.info("{}", nursingLevel);
        stringRedisTemplate.delete(NURSING_LEVEL_KEY + "listAll");

//        return nursingLevelMapper.updateNursingLevel(nursingLevel);
        return nursingLevelMapper.updateById(nursingLevel);
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        return nursingLevelMapper.deleteNursingLevelByIds(ids);
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        stringRedisTemplate.delete(NURSING_LEVEL_KEY + "listAll");
        return nursingLevelMapper.deleteNursingLevelById(id);
    }


    private static final String NURSING_LEVEL_KEY = "nursing_level:";

    /**
     * 查询护理等级列表
     *
     * @return
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelAll() {

        //根据key查询缓存
        String levelListJsonStr = stringRedisTemplate.opsForValue().get(NURSING_LEVEL_KEY + "listAll");
        if (StringUtils.hasText(levelListJsonStr)) {
            log.info("缓存存在, 直接返回");
            return JSONUtil.toList(levelListJsonStr, NursingLevelVo.class);
        }

        List<NursingLevel> nursingLevels = nursingLevelMapper.selectList(new LambdaQueryWrapper<NursingLevel>().eq(NursingLevel::getStatus, Constants.STATUS_ENABLED));

        List<NursingLevelVo> nursingLevelVos = nursingLevels.stream().map(nursingLevel -> BeanUtil.toBean(nursingLevel, NursingLevelVo.class)).collect(Collectors.toList());

        //存入缓存
        log.info("查询数据库，并将数据存入缓存");
        stringRedisTemplate.opsForValue().set(NURSING_LEVEL_KEY + "listAll", JSONUtil.toJsonStr(nursingLevelVos), Duration.ofMinutes(30));
        return nursingLevelVos;
    }
}
